<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<project default="writeSchemaSqlToFile" name="DdlUtils" basedir=".">
	<!-- Allow values set at the commandline or in the environment to override the defaults -->
	<property environment="env" />
	<!-- Load the jdbc properties as specified by the jdbc.properties.file variable-->
	<property file="jdbc.properties.postgresql" />
	<!-- Load the build properties -->
	<property file="build-ddl.properties" />

	<!-- These properties can be overriden on the commandline using -D -->
	<property name="catalogpattern" value="" />
	<property name="schemapattern" value="${ddlutils.schema}" />
	<property name="platform" value="${ddlutils.platform}" />

	<!-- The classpath used for running the tasks -->
	<path id="project-classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
			<include name="**/*.zip" />
		</fileset>
	</path>

	<taskdef classname="org.apache.ddlutils.task.DdlToDatabaseTask" name="ddlToDatabase" classpathref="project-classpath" />
	<taskdef classname="org.apache.ddlutils.task.DatabaseToDdlTask" name="databaseToDdl" classpathref="project-classpath" />

	<target name="createDb">
		<ddlToDatabase usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<createdatabase failonerror="false">
				<parameter name="ENCODING" value="'UTF8'" />
			</createdatabase>
		</ddlToDatabase>
	</target>

	<target name="writeSchemaToDb">
		<ddlToDatabase validatexml="false" usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="../model">
				<include name="**/*.xml" />
			</fileset>

			<writeschematodatabase alterdatabase="${alterdatabase}" dodrops="true" failonerror="false" />
		</ddlToDatabase>
	</target>

	<target name="writeSchemaSqlToFile">
		<ddlToDatabase validatexml="false" usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="../model">
				<include name="**/*.xml" />
			</fileset>

			<writeschemasqltofile alterdatabase="${alterdatabase}" dodrops="false" failonerror="false" createtables="true" createforeignkeys="false" outputfile="${sqloutputfile}" />
		</ddlToDatabase>
	</target>

	<target name="writeFkSqlToFile">
		<ddlToDatabase validatexml="false" usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="../model">
				<include name="**/*.xml" />
			</fileset>

			<writeschemasqltofile alterdatabase="${alterdatabase}" dodrops="false" failonerror="false" createtables="false" createforeignkeys="true" outputfile="${fkoutputfile}" />
		</ddlToDatabase>
	</target>

	<target name="writeSchemaToFile">
		<databaseToDdl usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<writeSchemaToFile outputfile="${schemaoutputfile}" />
		</databaseToDdl>
	</target>

	<target name="writeDataToFile">
		<databaseToDdl usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<writeDataToFile outputfile="${datafile}" />
		</databaseToDdl>
	</target>


	<target name="writeDataToDb">
		<ddlToDatabase usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="../model">
				<include name="**/*.xml" />
			</fileset>

			<writedatatodatabase usebatchmode="false" batchSize="20" ensureForeignKeyOrder="false" >
				<fileset dir="../data">
					<include name="**/*.xml" />
				</fileset>
			</writedatatodatabase>
		</ddlToDatabase>
	</target>

	<target name="splitSchemaFileByTable">
		<ddlToDatabase usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="..">
				<include name="${schemafiles}" />
			</fileset>

			<splitSchemaFileByTable destination="../model"/>
		</ddlToDatabase>
	</target>

	<target name="splitDataFileByTable">
		<ddlToDatabase usedelimitedsqlidentifiers="${delimitedsqlidentifiers}" catalogpattern="${catalogpattern}" schemapattern="${schemapattern}" databasetype="${platform}">
			<database driverclassname="${datasource.driverClassName}" url="${datasource.url}" username="${datasource.username}" password="${datasource.password}" />
			<fileset dir="../model">
				<include name="**/*.xml" />
			</fileset>

			<splitDataFileByTable destination="../data" datafile="${datafile}"/>
		</ddlToDatabase>
	</target>
</project>
